//
//  AIPhotoExtension+ToJson.swift
//  aif_photo_plugin
//
//  Created by 李旭峰 on 2021/4/11.
//

import Foundation
import Photos

extension PHAssetCollection {
  func toAifJson() -> [String:Any] {
    var json:[String:Any] = [:]
    json["localIdentifier"] = self.localIdentifier
    json["localizedTitle"] = self.localizedTitle
    json["assetCollectionType"] = self.assetCollectionType.rawValue
    json["assetCollectionSubtype"] = self.assetCollectionSubtype.rawValue
    json["createDate"] = self.startDate?.aiTransformString
    return json
  }
}


extension PHAsset {
  func toAifJson() -> [String:Any] {
    var json:[String:Any] = [:]
    json["localIdentifier"] = self.localIdentifier
    json["mediaType"] = self.mediaType.rawValue
    json["mediaSubtypes"] = self.mediaSubtypes.rawValue
    json["creationDate"] = self.creationDate?.aiTransformString
    return json
  }
}

extension PHFetchResult where ObjectType == PHAssetCollection {
  func toAifJson() -> [[String:Any]] {
    var jsonList:[[String:Any]] = []
    self.enumerateObjects { (album, _, _) in
      jsonList.append(album.toAifJson())
    }
    return jsonList
  }
}

extension PHFetchResult where ObjectType == PHAsset {
  func toAifJson() -> [[String:Any]] {
    var jsonList:[[String:Any]] = []
    self.enumerateObjects { (asset, _, _) in
      jsonList.append(asset.toAifJson())
    }
    return jsonList
  }
}


fileprivate extension Date {
  var aiTransformString: String {
    get {
      let formatter = DateFormatter()
      formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
      return formatter.string(from: self)
    }
  }
}
